using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace System {
	static class Program {
		public static string InnerText(string input) {
			return Regex.Replace(input,"<.+?>","");
		}
		public static void Check(string input,string pat,string csvInrRes) {
			Nod nod=new Nod(input);
			string[] result=nod.Dbls(pat);
			//string res=string.Join(",",result);
			string[] exp=csvInrRes.Split(',');
			if(result.Length!=exp.Length)
			{
				Console.WriteLine("False");
				return;
			}
			for(int i=0;i<result.Length;i++)
				if(InnerText(result[i])!=exp[i])
					Console.WriteLine("False "+i);
		}
		static void MainX() {
			Check("a<a >b<a>c<ab >d</ ab>e</ a>f</a>g","<a","bcdef,cde,d");
			Check("a<a>b<a>c<ab>d</ab>e</a>f</a>g","<a","bcdef,cde,d");
			Check("a<a>b<a>c<ab>d</ab>e</a>f</a>g","<a>","bcdef,cde");
			Check("a<a>b<ab>c</ab>d</a>e<a>f</a>g","<a","bcd,c,f");
			Check("a<a>b<ab>c</ab>d</a>e<a>f</a>g","<a>","bcd,f");
			Check("a<a>b<ab>c</ab>d</a>e<a>f</a>g","<ab","c");
			Check("a<a>b<b>c<c>d</c>e</b>f</a>g","<a","bcdef");
			Check("a<a>b<b>c<c>d</c>e</b>f</a>g","<b","cde");
			Check("a<a>b<b>c<c>d</c>e</b>f</a>g","<c","d");
			Check("a<a>b<a>c<a>d</a>e</a>f</a>g","<a","bcdef,cde,d");
		}
	}
	public class Nod {
		public static string InnerText(string input) {
			if(input==null)
				return "";
			return Regex.Replace(input,"<.+?>","",RegexOptions.Singleline).Trim();
		}
		public static string[] Dbls(string content,string pattern) { return new Nod(content).Dbls(pattern); }
		public static string Dbl(string content,string pattern) { return new Nod(content).Dbl(pattern); }
		public static string Rx(string content,string pattern) { return new Nod(content).Rx(pattern); }
		public static string[] Rxs(string content,string pattern) { return new Nod(content).Rxs(pattern); }
		public Nod(string content) { _content=content; }
		string _content;
		public string[] Rxs(string regex) {
			Regex rx=new Regex(regex,RegexOptions.Singleline);
			MatchCollection mc=rx.Matches(_content);
			string[] ret=new string[mc.Count];
			int cnt=0;
			foreach(Match mac in mc)
				ret[cnt++]=mac.Groups[mac.Groups.Count-1].Value;
			return ret;
		}
		public string Rx(string regex) {
			Regex rx=new Regex(regex,RegexOptions.Singleline);
			Match mc=rx.Match(_content);
			if(mc.Success)
				return mc.Groups[mc.Groups.Count-1].Value;
			return null;
		}
		public string[] Dbls(string pattern) {
			//int next=0;
			List<string> ret=new List<string>();
			Regex reg=new Regex(pattern);
			foreach(Match mat in reg.Matches(_content))
				ret.Add(Dbl(mat.Value,mat.Index));
			return ret.ToArray();
		}
		public string Dbl(string pattern) {
			Match mat=new Regex(pattern).Match(_content);
			if(mat.Success)
				return Dbl(pattern,mat.Index);
			return null;
		}
		private string Dbl(string pattern,int istart) {
			int lvl=0;
			string part=_content.Substring(istart,_content.IndexOf('>',istart)-istart+1).Replace("<","").Replace(">"," ").Split(' ')[0];
			//string part=pattern.Replace("<","").Replace(">"," ").Split(' ')[0];
			int ix0=istart;
			int startpos=_content.IndexOf(pattern,ix0);
			do
			{
				int ixs=_content.IndexOf("<"+part,ix0);
				if(ixs!=-1&&_content[ixs+part.Length+1]!=' '&&_content[ixs+part.Length+1]!='>')
				{
					ix0=ixs+part.Length+1;
					continue;
				}
				if(ixs==-1)
					ixs=int.MaxValue;
				//Match emat=System.Text.RegularExpressions.Regex.Match(_content.Substring(ix0),"</ *?"+part+">",RegexOptions.IgnoreCase);
				//int ixe=emat.Index+ix0;//????????????????
				int ixe=_content.IndexOf("</"+part+">",ix0);
				if(ixe==-1)
					ixe=int.MaxValue;
				else if(ixs<ixe)
					lvl++;
				else if(ixs>ixe)
					lvl--;
				else
					return "!";
				//Console.WriteLine(ix0+"|"+lvl);
				if(lvl==0)
					//return _content.Substring(startpos,ixe-startpos+emat.Value.Length);
					return _content.Substring(startpos,ixe-startpos+part.Length+3);
				ix0=Math.Min(ixs,ixe)+1;
			} while(lvl>0&&lvl<20);
			return "";
		}

		public static string Find(string str,string p) {
			int start=str.IndexOf(p);
			if(start==-1)
				return "";
			//string rest=str.Substring(start);

			for(int i=start;i>0;i--)
				if(str[i]=='<')
				{
					start=i;
					break;
				}
			string rest2=str.Substring(start);
			string firstT=str.Substring(start,str.IndexOf('>',start)-start+1).Replace("<","").Replace(">"," ").Split(' ')[0];

			string part=new Nod(str).Dbl("<"+firstT,start);

			return part;
		}

		public static string Inr(string content,string pattern) {

			string str=Dbl(content,pattern);

			str=str.Substring(str.IndexOf('>')+1);
			str=str.Substring(0,str.LastIndexOf('<'));


			return str.Trim();
		}
	}
}
